package com.ruoyi.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.ruoyi.domain.*;
import com.ruoyi.mapper.ZhwyChargeitemMapper;
import com.ruoyi.mapper.ZhwyHirerMapper;
import com.ruoyi.mapper.ZhwyShopsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.ZhwyChargedataMapper;
import com.ruoyi.service.IZhwyChargedataService;

/**
 * 收费数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-07
 */
@Service
public class ZhwyChargedataServiceImpl implements IZhwyChargedataService {
    @Autowired
    private ZhwyChargedataMapper zhwyChargedataMapper;

    //    收费项目名
    @Autowired
    private ZhwyChargeitemMapper zhwyChargeitemMapper;

    //    用户姓名
    @Autowired
    private ZhwyHirerMapper zhwyHirerMapper;

    //    商铺编号
    @Autowired
    private ZhwyShopsMapper zhwyShopsMapper;

    /**
     * 查询收费数据
     *
     * @param id 收费数据主键
     * @return 收费数据
     */
    @Override
    public ZhwyChargedata selectZhwyChargedataById(Long id) {
        return zhwyChargedataMapper.selectZhwyChargedataById(id);
    }

    /**
     * 查询收费数据列表
     *
     * @param zhwyChargedata 收费数据
     * @return 收费数据
     */
    @Override
    public List<ZhwyChargedata> selectZhwyChargedataList(ZhwyChargedata zhwyChargedata) {
        List<ZhwyChargedata> zhwyChargedatas = zhwyChargedataMapper.selectZhwyChargedataList(zhwyChargedata);
        for (ZhwyChargedata zh : zhwyChargedatas) {
            //获取关联收款项目
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(zh.getChargeitemId());
            ZhwyHirer zhwyHirer = zhwyHirerMapper.selectZhwyHirerById(zh.getHirerId());
            ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(zh.getShopsId());

//            zh.setChargeitemName(zhwyChargeitem.getName());
            if (zhwyChargeitem != null && zhwyChargeitem.getName() != null) {
                zh.setChargeitemName(zhwyChargeitem.getName());
                zh.setHirerName(zhwyHirer.getName());
                zh.setShopsCode(zhwyShops.getCode());

            } else {
                zh.setChargeitemName("居民供暖");
            }
        }
        return zhwyChargedatas;
//        return zhwyChargedataMapper.selectZhwyChargedataList(zhwyChargedata);
    }

    /**
     * 新增收费数据
     *
     * @param zhwyChargedata 收费数据
     * @return 结果
     */
    @Override
    public int insertZhwyChargedata(ZhwyChargedata zhwyChargedata) {
        String s = String.valueOf(UUID.randomUUID());
        zhwyChargedata.setBillNumber(s);
        return zhwyChargedataMapper.insertZhwyChargedata(zhwyChargedata);
    }

    /**
     * 修改收费数据
     *
     * @param zhwyChargedata 收费数据
     * @return 结果
     */
    @Override
    public int updateZhwyChargedata(ZhwyChargedata zhwyChargedata) {
        return zhwyChargedataMapper.updateZhwyChargedata(zhwyChargedata);
    }

    /**
     * 批量删除收费数据
     *
     * @param ids 需要删除的收费数据主键
     * @return 结果
     */
    @Override
    public int deleteZhwyChargedataByIds(Long[] ids) {
        return zhwyChargedataMapper.deleteZhwyChargedataByIds(ids);
    }

    /**
     * 删除收费数据信息
     *
     * @param id 收费数据主键
     * @return 结果
     */
    @Override
    public int deleteZhwyChargedataById(Long id) {
        return zhwyChargedataMapper.deleteZhwyChargedataById(id);
    }

    @Override
    public List<ZhwyChargedata> findByPaymentState(ZhwyChargedata zhwyChargedata) {
        List<ZhwyChargedata> list = zhwyChargedataMapper.selectZhwyChargedataListByPaymentState(zhwyChargedata);
        list.forEach(s->{
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(s.getChargeitemId());
            ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(s.getShopsId());
            s.setChargeitemName(zhwyChargeitem.getName());
            s.setShopsCode(zhwyShops.getCode());
        });
        return list;
    }

    @Override
    public List<ZhwyChargedata> findByBuildingId(Long id) {
        List<ZhwyChargedata> list = zhwyChargedataMapper.findByBuildingId(id);
        list.forEach(s->{
            ZhwyChargeitem zhwyChargeitem = zhwyChargeitemMapper.selectZhwyChargeitemById(s.getChargeitemId());
            ZhwyShops zhwyShops = zhwyShopsMapper.selectZhwyShopsById(s.getShopsId());
            s.setChargeitemName(zhwyChargeitem.getName());
            s.setShopsCode(zhwyShops.getCode());
        });
        return list;
    }


    //    批量新增
    @Override
    public int batchInsertZhwyChargedata(List<ZhwyChargedata> chargedataList) {
        return zhwyChargedataMapper.batchInsertZhwyChargedata(chargedataList);
    }




    /**
     * 获取每月的缴纳金额 -- 首页图标展示
     * @param currentTime --当前日期
     * @param beforeSix -- 六个月前
     * @return -- 符合的数据集
     */
    //
    @Override
    public List<ZhwyChargedata> queryZhwyDate(String currentTime, String beforeSix) {
        return zhwyChargedataMapper.queryZhwyDate(currentTime, beforeSix);
    }
    //
    @Override
    public List<ZhwyChargedata> queryZhwyMonthData(String start, String end) {
        return zhwyChargedataMapper.queryZhwyMonthData(start,end);
    }



}
